package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * Created by Rob on 11/09/2014.
 */
public class Menu {
    public Library theLibrary = new Library();
    public ArrayList<User> userBase = new ArrayList<User>();
    public Scanner scanner = new Scanner(System.in);
    public boolean loggedIn = false;
    public User currentUser = null;

    public Menu() {
        presetUserBase();
    }

    public int menuSelection(){
        System.out.println("Please type which the number of the option you would like to select:");
        System.out.println("1. User Login");
        System.out.println("2. List Books and Movies");
        System.out.println("3. Checkout book");
        System.out.println("4. Return Book");
        System.out.println("5. Checkout Movie");
        System.out.println("8. Report Borrowed Movies");
        System.out.println("9. Quit");
        String userChoice = scanner.next();
        return Integer.parseInt(userChoice);
    }

    public void allocateSelectionToSection(int selection) {
        boolean stop = false;
        switch (selection) {
            case 1:
                currentUser = userLogin();
                break;
            case 2:
                createLibrary();
                break;
            case 3:
                System.out.println("What is the name of the book you would like to check out?");
                checkoutABook();
                break;
            case 4:
                System.out.println("What is the name of the book you would like to return?");
                returnABook();
                break;
            case 5:
                System.out.println("What is the name of the movie you would like to rent?");
                checkoutAMovie();
                break;
            case 8:
                reportBorrowedBooks();
                break;
            case 9:
                System.out.println("Thanks for using the Biblioteca");
                stop = true;
                break;
            default:
                System.out.println("Select a valid option!");
                allocateSelectionToSection(menuSelection());
                break;
        }
        if(!stop) {
            allocateSelectionToSection(menuSelection());
        }
    }

    private User userLogin() {
        System.out.println("What is your library card number?");
        Scanner scan = new Scanner(System.in);
        String libraryCardNumber = scan.nextLine();
        System.out.println("What is your password?");
        String password = scan.nextLine();
        return loginTheUser(libraryCardNumber, password);

    }

    public User loginTheUser(String libraryCardNumberToCheck, String passwordToCheck){      //fix this method
        User theCurrentUser;
        for(int x = 0; x < userBase.size();x++){
            if(userBase.get(x).getLibraryCardNumber().equals(libraryCardNumberToCheck) && userBase.get(x).getPassword().equals(passwordToCheck)) {
                loggedIn = true;
                return theCurrentUser = userBase.get(x);
            }
        }
        System.out.println("Incorrect details, please try again.");
        return null;
    }

    public void createLibrary(){
        System.out.println();
        System.out.println("........................................................................................................................................");
        System.out.println("..USER: " + currentUser.getName() + "......................................." + "E-MAIL: " + currentUser.getEmail() + "......................................" + "PHONE: " + currentUser.getPhoneNumber() + "..");
        System.out.println("........................................................................................................................................");
        System.out.println();
        System.out.println("Here are the books and movies available:");
        System.out.println();
        System.out.println("........................................................................................................................................");
        System.out.println("..                              Title                   Author          Date Of Publication                                           ..");
        System.out.println("........................................................................................................................................");
        theLibrary.printBooks();
        System.out.println("........................................................................................................................................");
        System.out.println("..                              Title                   Director          Date Of Release            Movie Rating                     ..");
        System.out.println("........................................................................................................................................");
        theLibrary.printMovies();
        System.out.println("........................................................................................................................................");
        System.out.println("");
    }

    public void reportBorrowedBooks(){
        System.out.println();
        System.out.println("........................................................................................................................................");
        System.out.println("..USER: Admin...........................................................................................................................");
        System.out.println("........................................................................................................................................");
        System.out.println();
        System.out.println("Here are the movies which are out and the users who borrowed them:");
        System.out.println();
        System.out.println("........................................................................................................................................");
        System.out.println("..                User                              Title                   Author          Date Of Publication                       ..");
        System.out.println("........................................................................................................................................");
        System.out.println("..                                                                                                                                    ..");
        theLibrary.printRentedBooks(currentUser);
        System.out.println("..                                                                                                                                    ..");
        System.out.println("........................................................................................................................................");
        System.out.println("");
    }


    public void checkoutAMovie() {
        boolean hasBeenBorrowed = false;
        Scanner scan = new Scanner(System.in);
        String nameOfMovieToCheckout = scan.nextLine();
        ArrayList<Movie> movieList = theLibrary.moviesForBorrowing;
        for (int x = 0; x <  movieList.size(); x++) {
            if (movieList.get(x).getName().equals(nameOfMovieToCheckout)) {
                theLibrary.moviesToBeReturned.add(movieList.get(x));
                currentUser.addMovieToBorrowedList(movieList.get(x));
                theLibrary.moviesForBorrowing.remove(movieList.get(x));
                System.out.println("Thank you! Enjoy the movie");
                hasBeenBorrowed = true;
            }
        }
        if(!hasBeenBorrowed) {
            System.out.println("That movie is not available.");
        }
    }

    public void checkoutABook(){
        Scanner scan = new Scanner(System.in);
        String nameOfBookToCheckout = scan.nextLine();
        int bookToRemove = theLibrary.getBookNumber(getBookFromName(nameOfBookToCheckout));
        if(bookToRemove == -1){
            System.out.println("That book is not available.");
        } else {
            theLibrary.removeBook(bookToRemove);
            System.out.println("Thank you! Enjoy the book");
        }
    }

    public void returnABook(){
        Scanner scan = new Scanner(System.in);
        String nameOfBookToBeReturned = scan.nextLine();
        Book bookToAddBackToLibrary = getCheckoutBookFromName(nameOfBookToBeReturned);
        if (bookToAddBackToLibrary == null){
            System.out.println("That is not a valid book to return.");
        } else {
            theLibrary.booksForBorrowing.add(bookToAddBackToLibrary);
            System.out.println("Thank you for returning the book.");
        }
    }

    public Book getBookFromName(String nameOfBookToCheckout){
        for(Book bookInLibrary:theLibrary.booksForBorrowing){
            if (bookInLibrary.getName().equals(nameOfBookToCheckout)){
                return bookInLibrary;
            }
        }
        return null;
    }

    public Book getCheckoutBookFromName(String nameOfBookToCheckout){
        for(Book bookToBeReturned:theLibrary.booksToBeReturned){
            if (bookToBeReturned.getName().equals(nameOfBookToCheckout)){
                return bookToBeReturned;
            }
        }
        return null;
    }

    public void presetUserBase(){
        userBase.add(new User("(507)-3157", "John", "johnpw", "john1234@gmail.com", 99283942));
        userBase.add(new User("(955)-8423", "Jane", "janepw", "jane1234@gmail.com", 99283832));
        userBase.add(new User("(483)-2198", "Michael", "michaelpw", "michael1@gmail.com", 99283722));
        userBase.add(new User("(743)-5896", "Michelle", "michellepw", "michelle@gmail.com", 99283612));
    }
}
